﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using UIFrameWork;
using UnityEngine;

public class AssetBundlePathResolver : Singleton<AssetBundlePathResolver>
{
    private DirectoryInfo cacheDir;
    private string cachePath;
    private string cachePathWWW;
    private StringBuilder pathSB = new StringBuilder();

    public virtual string GetEditorModePath(string abName)
    {
        abName = abName.Replace(".", "/");
        int length = abName.LastIndexOf("/"); //路径中没有/
        if (length == -1)
        {
            return abName;
        }
        return abName.Substring(0, length) + "." + abName.Substring(length + 1);
//        return $"{abName.Substring(0, length)}.{abName.Substring(length + 1)}";
    }

    public string BundeSaveDieName()
    {
        return "AssetBundles";
    } 

    /// <summary>
    /// 打包android保存的路径
    /// </summary>
    /// <returns></returns>
    public string AndroidBundleSavePath()
    {
        return "Assets/StreamingAssets/update/Android/" + this.BundeSaveDieName();
    }

    public virtual string AndroidHashCacheSaveFile()
    {
        return "Assets/AssetBundles/Android/cache.txt";
    }


    public virtual string BundleCacheDir()
    {
        if (this.cacheDir != null)
        {
            return this.cacheDir.FullName;
        }
        return string.Empty;
    }


    public string DefaultBundleSavePath()
    {
        return  ("Assets/StreamingAssets/update/" + this.BundeSaveDieName());
    }

    public virtual string DefauleHashCacheSaveFile()
    {
        return  "Assets/AssetBundles/cache.txt";
    }


    public virtual string DependFileName()
    {
        return  "dep.all";
    }


    public string IOSBundleSavePath()
    {
        return ("Assets/StreamingAssets/update/IOS/" + this.BundeSaveDieName());
    }

    public string IOSHashCacheSaveFile()
    {
        return "Assets/AssetBundles/IOS/cache.txt";
    }
    

    public virtual string GetBundleSourceFile(string path, bool forWWW = true)
    {
        if (!forWWW) //不是www表示是本地加载
        {
            if (this.cachePath == null)
            {
                RuntimePlatform runtimePlatform = Application.platform;
                if (runtimePlatform != RuntimePlatform.IPhonePlayer)
                {
                    if (runtimePlatform != RuntimePlatform.Android)
                    {
                        //pc平台
                        this.cachePath = Application.dataPath + "/StreamingAssets/update/" + this.BundeSaveDieName()+"/";
                    }
                    else
                    {
                        //android平台
                        this.cachePath =Application.dataPath+"!assets/update/Android/"+this.BundeSaveDieName()+"/";
                    }
                }
                else
                {
                    //ios平台
                    this.cachePath = Application.dataPath+"/Raw/update/IOS/"+this.BundeSaveDieName()+"/";
                }
            }
            this.pathSB.Length = 0;
            this.pathSB.Append(cachePath);
        }
        else
        {
            if (this.cachePathWWW == null)
            {
                RuntimePlatform runtimePlatform = Application.platform;
                if (runtimePlatform != RuntimePlatform.IPhonePlayer)
                {
                    if (runtimePlatform != RuntimePlatform.Android)
                    {
                        this.cachePathWWW = "file://" + Application.dataPath + "/StreamingAssets/update/" +
                                            this.BundeSaveDieName() + "/";
                        
                    }
                    else
                    {
                        this.cachePathWWW = "file://" + Application.dataPath + "!assets/update/Android/" +
                                            this.BundeSaveDieName() + "/";
                    }
                }
                else
                {
                    this.cachePathWWW = "file://" + Application.dataPath + "/Raw/update/IOS" +
                                        this.BundeSaveDieName() + "/";
                }
            }
            this.pathSB.Length = 0;
            this.pathSB.Append(cachePathWWW);
        }
        this.pathSB.Append(path);
        return this.pathSB.ToString();
    }
}


















































